﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Xml.Linq;

namespace BAR_POS
{
    public partial class TableChart : DevExpress.XtraEditors.XtraUserControl
    {
        Dictionary<int, SimpleButton> TableButtonList = new Dictionary<int, SimpleButton>();

        double subtotal = 0.00;
        double taxrate = 0.0825;
        double tax = 0.00;
        double autogratrate = 0.00;
        double autograt = 0.00;
        double grat = 0.00;
        public static double total = 0.00;
        double DiscountRate = 0.00;
        double Discount = 0.00;
        public static double paidtotal = 0.00;

        int ReceiptNumber = 0;

        string receiptTableName;
        string ServerFullName;


        public TableChart()
        {
            StaticProperties.GetDateCode();
            InitializeComponent();
            int x = 1;
            // read in the entire document
            var document = XDocument.Parse(StaticProperties.TableChartXML);//.Load(filePath);

            var champs = document.Descendants("SimpleButton")
                .Select(e => new { text = e.Element("Text").Value, X = e.Element("X").Value, Y = e.Element("Y").Value, Width = e.Element("Width").Value, Height = e.Element("Height").Value });

            foreach (var champ in champs)
            {
                SimpleButton test = new SimpleButton();
                
                //int locationXextra = (340 / 2) + 50;
                test.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                test.Name = "btn" + x.ToString();
                test.Text = champ.text;
                test.Width = Convert.ToInt32(champ.Width);
                test.Height = Convert.ToInt32(champ.Height);
                test.Location = new Point(Convert.ToInt32(champ.X), Convert.ToInt32(champ.Y) - 150);
                test.MouseClick += new MouseEventHandler(btn_MouseClick);
                test.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                test.Appearance.FontStyleDelta = FontStyle.Bold;
                //MessageBox.Show(x.ToString());
                TableButtonList.Add(x, test);
                this.Controls.Add(test);

                x++;
                
            }
        }

        void btn_MouseClick(object sender, MouseEventArgs e)
        {
            var button = (SimpleButton)sender;
            StaticProperties.TableName = button.Text;

            //if (button.Text == "OPEN TICKETS")
            //{
            //    MainProgram._Main.panelControl1.Controls.Clear();
            //    MainProgram._Main.panelControl1.Controls.Add(new OpenTickets());
            //}
            //else
            //{
                if (button.ForeColor == Color.Blue)
                {
                    int ModReceipt = 0; ;
                    foreach (DataRow Receipt in DataTableClass.Receipt.Rows)
                    {
                        if(!Convert.ToBoolean(Receipt["ReceiptStatus"].ToString()))
                        {
                            if(Receipt["ReceiptTableName"].ToString().Trim().ToUpper() == button.Text.ToUpper())
                            {
                                ModReceipt = Convert.ToInt32(Receipt["ReceiptNo"].ToString());
                            }
                        }
                    }

                    MainProgram._Main.panelControl1.Controls.Clear();
                    MainProgram._Main.panelControl1.Controls.Add(new OrderEntryScreen(ModReceipt));

                }
                else
                {
                    MainProgram._Main.panelControl1.Controls.Clear();
                    MainProgram._Main.panelControl1.Controls.Add(new OrderEntryScreen());
                }
            //}
        }

        private void TableChart_Load(object sender, EventArgs e)
        {
            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {
                //if (item["ReceiptNo"].ToString().StartsWith(StaticProperties.DateCode.ToString()))
                //{
                    if (item["ReceiptServerID"].ToString().Trim() == StaticProperties.ServerID.ToString())
                    {
                        if (Convert.ToBoolean(item["ReceiptStatus"].ToString()) == false)
                        {
                            foreach (KeyValuePair<int, SimpleButton> buttonList in TableButtonList)
                            {
                                if (buttonList.Value.Text == item["ReceiptTableName"].ToString().Trim())
                                {
                                    buttonList.Value.ForeColor = Color.Blue;
                                }
                            }
                        }
                    }
                //}
            }

            GetOpenTickets(StaticProperties.ServerID);
        }

        private void GetOpenTickets(int ServerID)
        {
            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {
                //if (StaticProperties.EmployeeTitle == "OWNER")
                //{
                //    ListViewItem lvi = new ListViewItem((item["ReceiptNo"]).ToString());

                //    lvi.SubItems.Add((item["ReceiptTableName"]).ToString());
                //    string subtotal = (item["ReceiptSubTotal"]).ToString();
                //    double subtotal1 = Convert.ToDouble(subtotal);
                //    lvi.SubItems.Add(subtotal1.ToString("C"));

                //    listView1.Items.Add(lvi);
                //}

                //else 
                if (item["ReceiptServerID"].ToString().Trim() == ServerID.ToString())
                {
                    ListViewItem lvi = new ListViewItem((item["ReceiptNo"]).ToString());

                    lvi.SubItems.Add((item["ReceiptTableName"]).ToString());
                    string subtotal = (item["ReceiptSubTotal"]).ToString();
                    double subtotal1 = Convert.ToDouble(subtotal);
                    lvi.SubItems.Add(subtotal1.ToString("C"));

                    listView1.Items.Add(lvi);
                }
            }

            foreach (DataRow item in DataTableClass.EmployeeLogin.Rows)
            {
                if (item["EmployeeID"].ToString().Trim() == ServerID.ToString())
                {
                    ServerFullName = item["EmployeeFullName"].ToString().Trim();
                }
            }
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            listView2.Items.Clear();

            if (listView1.SelectedIndices.Count <= 0)
            {
                //MessageBox.Show("Please Select a Receipt to Edit");
            }
            else
            {
                int intselectedindex = listView1.SelectedIndices[0];
                if (intselectedindex >= 0)
                {
                    String text = listView1.Items[intselectedindex].Text;
                    ReceiptNumber = Convert.ToInt32(text);


                    foreach (DataRow item in DataTableClass.ReceiptData.Rows)
                    {
                        if (item["ReceiptNo"].ToString().Trim() == ReceiptNumber.ToString())
                        {


                            string subtotal = (item["ReceiptItemTotal"]).ToString();
                            double subtotal1 = Convert.ToDouble(subtotal);


                            ListViewItem lvi = new ListViewItem((item["ReceiptQty"]).ToString().Trim());
                            lvi.SubItems.Add((item["ReceiptDescription"]).ToString().Trim());
                            lvi.SubItems.Add(subtotal1.ToString("C"));

                            listView2.Items.Add(lvi);

                        }
                    }

                    foreach (DataRow item in DataTableClass.Receipt.Rows)
                    {
                        if (item["ReceiptNo"].ToString().Trim() == ReceiptNumber.ToString())
                        {
                            receiptTableName = (item["ReceiptTableName"]).ToString();

                            string Autograt = item["ReceiptAutoGrat"].ToString().Trim();
                            double Autograt1 = Convert.ToDouble(Autograt);

                            autograt = Autograt1;
                            if (autograt != 0)
                            {
                                autogratrate = 0.18;
                            }

                            string Grat = item["ReceiptGrat"].ToString().Trim();
                            double Grat1 = Convert.ToDouble(Grat);

                            grat = Grat1;

                            string paid = item["ReceiptGrandTotal"].ToString().Trim();
                            double paid1 = Convert.ToDouble(paid);

                            paidtotal = paid1;

                        }
                    }
                    CalculateTicket(ReceiptNumber);
                }
            }
        }

        private double GetSubTotal(int ReceiptNo)
        {
            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {
                //lblReceiptNumber.Text = ReceiptNumber.ToString();

                if (item["ReceiptNo"].ToString().Trim() == ReceiptNumber.ToString())
                {
                    //lblTableName.Text = item["ReceiptTableName"].ToString().Trim();
                    string subtotal1 = (item["ReceiptSubTotal"]).ToString();
                    subtotal = Convert.ToDouble(subtotal1);
                }
            }
            return subtotal;
        }

        private void CalculateTicket(int ReceiptNo)
        {
            subtotal = GetSubTotal(ReceiptNo);
            //MessageBox.Show(subtotal.ToString("C"));
            lblSubTotal.Text = subtotal.ToString("C");

            tax = subtotal * taxrate;
            lblTaxTotal.Text = tax.ToString("C");

            autograt = (subtotal + tax) * autogratrate;
            if (autograt != 0.00)
            {
                lblAutoGratTotal.Text = autograt.ToString("C");
            }
            else
            {
                lblAutoGratTotal.Text = "";
            }

            if (DiscountRate != 0)
            {
                double amount = (subtotal * DiscountRate);
                Discount = amount;
                lblDiscountTotal.Text = Discount.ToString("C");
            }
            else
            {
                lblDiscountTotal.Text = "";
            }

            total = (subtotal + tax + autograt - Discount);
            lblBillTotal.Text = total.ToString("C");

            if (paidtotal == 0)
            {
                paidtotal = total + grat;
            }
            if (grat != 0)
            {
                lblGratTotal.Text = grat.ToString("C");
                lblPaidTotal.Text = paidtotal.ToString("C");
            }
            else
            {
                lblGratTotal.Text = "";
                lblPaidTotal.Text = "";
            }
        }

        private void btnPrintTicket_Click(object sender, EventArgs e)
        {
            PrintingClass.PrintReceiptForTransaction(ReceiptNumber.ToString(), ServerFullName, receiptTableName);
        }

        private void btnDrinkDatabase_Click(object sender, EventArgs e)
        {
            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new DrinkDatabaseScreen());
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new BeverageDatabaseScreen());
        }
    }
}
